<script setup lang="ts">
import type { FormInstance, FormRules } from 'element-plus'
import {ref, reactive} from "vue";
import {User, Lock} from '@element-plus/icons-vue'
import {userRules} from "@/utils/validate";
import {useUserStore} from "@/stores/user";
import {ElMessage} from "element-plus";
import router from "@/router";

const userStore = useUserStore()

// 定义表单实例
const loginFormRef = ref<FormInstance>()
// 定义登录表单数据
const loginForm = reactive({
username: '',
password: ''
})
// 定义表单校验规则
const loginFormRules = reactive({
username: userRules.username,
password: userRules.password
})
// 按钮的加载状态
const isLoading = ref(false)

async function submitLoginForm(formEl: FormInstance | undefined) {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      // 校验成功，发送登录请求
      try {
        isLoading.value = true
        await userStore.login(loginForm)
        // 登录成功，跳转
        ElMessage.success('登录成功')
        await router.push('/')
      }catch (e) {
        return
      }finally {
        isLoading.value = false
      }
    }
  })
}
</script>

<template>
  <div class="login-container">
    <el-container>
      <el-aside></el-aside>
      <el-main>
        <a href="#">
          <img src="@/assets/images/logo.svg" alt="" />
        </a>
        <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginFormRules"
            @keyup.enter="submitLoginForm(loginFormRef)">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" autofocus>
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" show-password>
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button
                type="primary"
                @click="submitLoginForm(loginFormRef)"
                :loading="isLoading"
            >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
  </div>
</template>

<style lang="less" scoped>
  .login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-image: linear-gradient(to left, #00c6ff, #0072ff);
  }

  .el-container {
    flex: none;
    width: 1000px;
    height: 400px;
    background-color: #fff;
    border-radius: 20px;
    overflow: hidden;
  }

  .el-aside {
    width: 50%;
    background-image: url('@/assets/images/login.png');
    background-size: cover;
    background-position: right center;
  }

  .el-main {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 50%;

    img {
      width: 100px;
    }

    .el-form {
      display: flex;
      flex-direction: column;
      justify-content: space-evenly;
      height: 100%;

      .el-input {
        width: 200px;
      }

      .el-button {
        margin: 0 auto;
        width: 100px;
      }
    }
  }
</style>
